package com.orderplus.opshop.synces.service.elasticsearch.impl;

import com.alibaba.fastjson.JSON;
import com.orderplus.opshop.common.util.ValueUtil;
import com.orderplus.opshop.core.data.request.RequestParam;
import com.orderplus.opshop.core.data.response.ResponseData;
import com.orderplus.opshop.core.template.TransTemplate;
import com.orderplus.opshop.synces.bean.ProductDetailModel;
import com.orderplus.opshop.synces.request.OpEsIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * author:maihj 2018.9.21
 * <p>
 * OpEsIndexRequest     			原来的请求
 * Boolean     						原来的响应
 * BulkRequest   					转换后的请求
 * ResponseData<Boolean>, Boolean> 	转换后的响应
 */
@Service
public class OpIndexService extends TransTemplate<OpEsIndexRequest, Boolean, BulkRequest, ResponseData<Boolean>, Boolean> {
	@Autowired
	private RestHighLevelClient restHighLevelClient;

	@Override
	public ResponseData<Boolean> call(RequestParam<BulkRequest> requestParam) {
		ResponseData<Boolean> responseData = new ResponseData<>();

		BulkRequest bulkRequest = requestParam.getRequestBody();
		try {
			restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
		} catch (IOException e) {
			e.printStackTrace();
			responseData = ResponseData.fail();
			responseData.setData(false);
			return responseData;
		}

		responseData = ResponseData.success();
		responseData.setData(true);

		return responseData;
	}

	@Override
	protected BulkRequest transformRequest(OpEsIndexRequest request) {
		List<ProductDetailModel> requestList = request.getData();
//		List<Map<String, Object>> requestList = request.getColumns();

		BulkRequest bulkRequest = new BulkRequest();

		for (int i = 0; i < requestList.size(); i++) {
//			Map<String, Object> requestParam = requestList.get(i);
//			String id = ValueUtil.toStr(requestParam.get(request.getPrimaryKeyName()));
			ProductDetailModel requestParam = requestList.get(i);
			if (null != requestParam) {
				String id = ValueUtil.toStr(requestParam.getProductId());
				IndexRequest indexRequest = new IndexRequest(request.getTableName(), request.getEsType(), id);
//				判断,字符长度是否超过2k,如超过,会无法插入,只能限制
//				BytesArray source = new BytesArray(requestParam.toString());
//				if(source.length() > 2048){
//					requestParam.setPropMap(null);
//					requestParam.setSkuMap(null);
//					requestParam.setAttributeMap(null);
//				}
				indexRequest.source(JSON.toJSONString(requestParam), XContentType.JSON);

				bulkRequest.add(indexRequest);
			}
		}
		System.out.println("index request  execute Description:" + bulkRequest.getDescription() + ",RefreshPolicy:" + bulkRequest.getRefreshPolicy().getValue() + ",ParentTask:" + bulkRequest.getParentTask());

		return bulkRequest;
	}

	@Override
	protected Boolean transformerResponse(Boolean aBoolean, RequestParam<OpEsIndexRequest> requestParam) {
		return true;
	}

}
